<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.blurry"
          clearable
          size="small"
          placeholder="输入感受名称搜索"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!--表单组件-->
    <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="感受名称" prop="name">
          <el-input v-model="form.name" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" style="width: 370px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="感受名称" />
      <el-table-column prop="remark" label="备注" />
      <el-table-column prop="createTime" label="创建时间" width="135" />
      <el-table-column
        v-if="checkPer(['admin','trainFeel:edit','trainFeel:del'])"
        label="操作"
        width="150px"
        align="center"
      >
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import crudTrainFeel from '@/api/trainFeel'
import { mapActions } from 'vuex'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, name: null, remark: null }
export default {
  name: 'TrainFeel',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ 
      title: '主观感受', 
      url: 'api/trainFeel', 
      idField: 'id', 
      sort: 'id,desc',
      crudMethod: { ...crudTrainFeel }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'trainFeel:add'],
        edit: ['admin', 'trainFeel:edit'],
        del: ['admin', 'trainFeel:del']
      },
      rules: {
        name: [
          { required: true, message: '感受名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        remark: [
          { max: 200, message: '备注长度不能超过 200 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.crud.msg.add = '新增成功'
    this.crud.msg.edit = '修改成功'
    this.crud.msg.del = '删除成功'
  },
  methods: {
    ...mapActions('feel', ['syncFeelData']),
    
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      // 可以在这里添加一些初始化操作
    },
    // 新增前做的操作
    [CRUD.HOOK.beforeToAdd]() {
      // 新增前的操作
    },
    // 编辑前做的操作
    [CRUD.HOOK.beforeToEdit](crud, form) {
      // 编辑前的操作
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      return true
    },
    // 新增成功后同步数据到 Vuex
    [CRUD.HOOK.afterAddCU](crud, data) {
      this.syncFeelData({ action: 'add', data: crud.form, response: data })
    },
    // 编辑成功后同步数据到 Vuex
    [CRUD.HOOK.afterEditCU](crud, data) {
      this.syncFeelData({ action: 'edit', data: crud.form, response: data })
    },
    // 删除成功后同步数据到 Vuex
    [CRUD.HOOK.afterDelCU](crud, data) {
      this.syncFeelData({ action: 'del', data: data })
    },
    // 获取数据成功后同步到 Vuex
    [CRUD.HOOK.afterRefresh](crud, data) {
      this.syncFeelData({ action: 'list', data: crud.params, response: data })
    }
  }
}
</script>

<style scoped>

</style> 